package com.zfzs.post.modules.flow.service.impl;

import com.zfzs.post.modules.flow.domain.FlowWithdraw;
import com.zfzs.post.modules.flow.repository.FlowWithdrawRepository;
import com.zfzs.post.modules.flow.service.FlowWithdrawService;
import com.zfzs.post.modules.flow.service.dto.FlowWithdrawDTO;
import com.zfzs.post.modules.flow.service.dto.FlowWithdrawQueryCriteria;
import com.zfzs.post.modules.flow.service.mapper.FlowWithdrawMapper;
import com.zfzs.post.utils.ValidationUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import java.util.Optional;
import cn.hutool.core.lang.Snowflake;
import cn.hutool.core.util.IdUtil;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import com.zfzs.post.utils.PageUtil;
import com.zfzs.post.utils.QueryHelp;

/**
* @author ck
* @date 2019-09-09
*/
@Service
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class FlowWithdrawServiceImpl implements FlowWithdrawService {

    @Autowired
    private FlowWithdrawRepository flowWithdrawRepository;

    @Autowired
    private FlowWithdrawMapper flowWithdrawMapper;

    @Override
    public Object queryAll(FlowWithdrawQueryCriteria criteria, Pageable pageable){
        Page<FlowWithdraw> page = flowWithdrawRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(flowWithdrawMapper::toDto));
    }

    @Override
    public Object queryAll(FlowWithdrawQueryCriteria criteria){
        return flowWithdrawMapper.toDto(flowWithdrawRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    public FlowWithdrawDTO findById(Long id) {
        Optional<FlowWithdraw> flowWithdraw = flowWithdrawRepository.findById(id);
        ValidationUtil.isNull(flowWithdraw,"FlowWithdraw","id",id);
        return flowWithdrawMapper.toDto(flowWithdraw.get());
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public FlowWithdrawDTO create(FlowWithdraw resources) {
        Snowflake snowflake = IdUtil.createSnowflake(1, 1);
        resources.setId(snowflake.nextId()); 
        return flowWithdrawMapper.toDto(flowWithdrawRepository.save(resources));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(FlowWithdraw resources) {
        Optional<FlowWithdraw> optionalFlowWithdraw = flowWithdrawRepository.findById(resources.getId());
        ValidationUtil.isNull( optionalFlowWithdraw,"FlowWithdraw","id",resources.getId());
        FlowWithdraw flowWithdraw = optionalFlowWithdraw.get();
        flowWithdraw.copy(resources);
        flowWithdrawRepository.save(flowWithdraw);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(Long id) {
        flowWithdrawRepository.deleteById(id);
    }
}